package com.jy.rhin.facade.infra.controller.notification;

import com.jy.framework.base.entity.Result;
import com.jy.rhin.application.infra.NotificationApplicationService;
import com.jy.rhin.domain.notification.publish.PublishMessageDomainService;
import com.jy.rhin.infra.repo.notfication.publish.query.PublishMessageQueryHandler;
import com.jy.rhin.sdk.dto.notification.req.CountDateNotificationDto;
import com.jy.rhin.sdk.dto.notification.req.SavePublishMessageReqDto;
import com.jy.rhin.sdk.dto.notification.resp.CountDateNotificationRespDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WeiQ
 * @date 2022/6/30 13:55
 * 发布消息记录
 */
@Api(tags = "事件总线api", value = "发布消息记录")
@RestController
@RequestMapping("/infra/notification/publish")
@Slf4j
public class PublishController {


    @Resource
    NotificationApplicationService notificationApplicationService;

    @Resource
    PublishMessageQueryHandler publishMessageQueryHandler;

    @Resource
    PublishMessageDomainService publishMessageDomainService;

    @ApiOperation("发布消息记录-保存发送消息记录")
    @PostMapping("/savePublishMessage")
    public Result<Boolean> savePublishMessage(@RequestBody SavePublishMessageReqDto req) {
        notificationApplicationService.publishMessage(req.getPublishId(), req.getPublishResult(), req.getFailReason());
        return Result.success(true);
    }

    @ApiOperation("消息记录-统计指定日期通知次数")
    @RequestMapping(value = "/countDateNotification", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<CountDateNotificationRespDto>> countDateNotification(CountDateNotificationDto dto) {
        List<CountDateNotificationRespDto> countList = publishMessageQueryHandler.countDateNotification(dto);
        return Result.success(countList);
    }

    @ApiOperation("消息记录-重置指定日期通知次数")
    @GetMapping(value = "/resetStatistics")
    public Result<Boolean> resetStatistics(CountDateNotificationDto dto) {
        publishMessageDomainService.resetStatistics(dto.getStartDate(), dto.getEndDate());
        return Result.success(true);
    }

    @ApiOperation(value = "消息记录-消息通知数缓存+1", hidden = true)
    @GetMapping(value = "/addNowDateTotalCache")
    public Result<Boolean> addNowDateTotalCache() {
        publishMessageDomainService.addNowDateTotalCache();
        return Result.success(true);
    }

    @ApiOperation(value = "消息记录-删除当天的消息通知数缓存", hidden = true)
    @GetMapping(value = "/removeNowDateTotalCache")
    public Result<Boolean> removeNowDateTotalCache() {
        publishMessageDomainService.removeNowDateTotalCache();
        return Result.success(true);
    }

    @ApiOperation(value = "消息记录-设置当天的消息通知数缓存", hidden = true)
    @GetMapping(value = "/setNowDateTotalCache")
    public Result<Boolean> setNowDateTotalCache() {
        publishMessageDomainService.setNowDateTotalCache();
        return Result.success(true);
    }

    @ApiOperation(value = "消息记录-获取当天的消息通知数缓存", hidden = true)
    @GetMapping(value = "/getNowDateTotalCache")
    public Result<Integer> getNowDateTotalCache() {
        Integer total = publishMessageDomainService.getNowDateTotalCache();
        return Result.success(total);
    }

    @ApiOperation(value = "消息记录-保存昨天的消息通知数量", hidden = true)
    @GetMapping(value = "/saveYesterdayTotal")
    public Result<Boolean> saveYesterdayTotal() {
        publishMessageDomainService.saveYesterdayTotal();
        return Result.success(true);
    }
}
